package com.chuangshu.chuangshuteam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangshu.chuangshuteam.dto.TeamInfoDTO;
import com.chuangshu.chuangshuteam.dto.TeamMemberDTO;
import com.chuangshu.chuangshuteam.dto.TeamMemberListDTO;
import com.chuangshu.chuangshuteam.dto.TeamUpdateRequest;
import com.chuangshu.chuangshuteam.entity.Team;
import com.chuangshu.chuangshuteam.mapper.TeamMapper;
import com.chuangshu.chuangshuteam.service.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {

    @Override
    public TeamInfoDTO getTeamInfo(Long teamId) {
        Team team = baseMapper.selectById(teamId);
        if (team == null) {
            return null;
        }

        TeamInfoDTO dto = new TeamInfoDTO();
        BeanUtils.copyProperties(team, dto);
        
        // 填充统计数据
        dto.setMemberCount(baseMapper.countTotalMembers(teamId));
        dto.setAdminCount(baseMapper.countAdmins(teamId));
        return dto;
    }

    @Override
    @Transactional
    public boolean updateTeamInfo(Long teamId, TeamUpdateRequest request) {
        Team team = baseMapper.selectById(teamId);
        if (team == null) {
            return false;
        }

        if (request.getName() != null) {
            team.setName(request.getName());
        }
        if (request.getAnnouncement() != null) {
            team.setAnnouncement(request.getAnnouncement());
        }
        team.setUpdatedAt(LocalDateTime.now());
        
        return baseMapper.updateById(team) > 0;
    }

    @Override
    @Transactional
    public String resetInviteCode(Long teamId) {
        Team team = baseMapper.selectById(teamId);
        if (team == null) {
            return null;
        }

        // 生成8位随机邀请码
        String inviteCode = UUID.randomUUID().toString().substring(0, 8).toUpperCase();
        team.setInviteCode(inviteCode);
        team.setUpdatedAt(LocalDateTime.now());
        
        baseMapper.updateById(team);
        return inviteCode;
    }

    @Override
    public TeamMemberListDTO getTeamMembers(
            Long teamId,
            Integer page,
            Integer size,
            String direction,
            Long currentUserId) {
        
        // 处理分页参数
        page = page == null ? 1 : page;
        size = size == null ? 20 : size;
        Page<TeamMemberDTO> pageParam = new Page<>(page, size);

        // 查询成员列表
        IPage<TeamMemberDTO> memberPage = baseMapper.selectTeamMembers(pageParam, teamId, direction);
        
        // 标记当前用户
        memberPage.getRecords().forEach(member -> {
            member.setCurrentUser(member.getUserId().equals(currentUserId));
        });

        // 统计方向分布
        Map<String, Integer> directionStats = baseMapper.countByDirection(teamId);

        // 组装返回结果
        TeamMemberListDTO result = new TeamMemberListDTO();
        result.setTotal(memberPage.getTotal());
        result.setPage(page);
        result.setSize(size);
        result.setDirectionStats(directionStats);
        result.setMembers(memberPage.getRecords());
        
        return result;
    }
}